﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Transactions;
using System.ServiceModel;
using ListenMusic.Friend_ServiceReference;
using ListenMusic.Message_ServiceReference;
using ListenMusic.Models.Reposistory.User;
namespace ListenMusic.Controllers.User
{
    public class FriendController : Controller
    {
        //
        // GET: /Friend/
        FriendResposistory _friend;
        MessageResposistory _mess;
        public FriendController()
        {
            _friend = new FriendResposistory();
            _mess = new MessageResposistory();
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddFriend(string userB)
        {
            try
            {
                MessageRecipient mr = new MessageRecipient();
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    try
                    {
                        Friend f = new Friend();
                        f.UserA = Session["user"].ToString();
                       
                            f.UserB = userB;
                        Message m = new Message();
                     
                        DateTime d = DateTime.Now;
                        m._Message = Session["user"].ToString() + "Muốn Kết Bạn Với Bạn";
                     
                        m.Created = d.Date;
                        m.UserSend = Session["user"].ToString();

                        _friend.AddFriend(f);
                        _mess.AddMessage(m);
                      
                        ts.Complete();
                      
                    }
                    catch (Exception ex)
                    {
                        ts.Dispose();
                        throw new Exception(ex.Message);
                    }
                }
                mr.MessageStyle = "Friend";
                mr.UserReceive = userB;
                _mess.AddMessageRecipient(mr);
                     
            }
            catch (FaultException ex)
            {
                throw ex;
            }
            return View();
        }

    }
}
